package com.yanqu.road.server.cmd.activity.oldplayerreturn;

import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.OldPlayerReturnProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.activity.oldplayerreturn.OldPlayerReturnMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author ：zfr
 * @date ：2021/12/8 14:48
 */
@Cmd(code = GameProtocol.S_CROSS_BIND_RETURN_ROLE, desc = "跨服来绑定回归角色")
public class CrossBindRoleCmd implements Command {
    private long oriServerId;
    private long oriUserId;
    private String oriOpenId;
    private PlayerProto.PlayerBaseTempMsg oriUserInfo;

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        OldPlayerReturnProto.BindReturnRoleCrossReqMsg reqMsg = OldPlayerReturnProto.BindReturnRoleCrossReqMsg.parseFrom(packet.getBytes());
        oriServerId = reqMsg.getOriServerId();
        oriUserId = reqMsg.getOriUserId();
        oriOpenId = reqMsg.getOriOpenId();
        oriUserInfo = reqMsg.getOriUserInfo();
        int ret = OldPlayerReturnMgr.crossBindPlayer(packet.getPlayerId(), oriServerId, oriUserId ,oriOpenId, oriUserInfo);
        if (ret != 0) {
            OldPlayerReturnProto.BindReturnRoleSuccessRespCrossMsg.Builder respMsg = OldPlayerReturnProto.BindReturnRoleSuccessRespCrossMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setOriServerId(oriServerId);
            respMsg.setOriUserId(oriUserId);
            YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_CROSS_BIND_RETURN_ROLE_SUCCESS_RESP, respMsg);
            GamePlayerMgr.sendPacket(packet.getPlayerId(), message);
        }
    }
}
